# -*- coding: utf-8 -*-

import os

import leancloud
from wsgiref import simple_server

from app import app
from cloud import engine

# if not os.environ.get('VIRTUAL_ENV', '').startswith('F:'):
    # import newrelic.agent
    # newrelic.agent.initialize('newrelic.ini')

APP_ID = os.environ['LC_APP_ID']
MASTER_KEY = os.environ['LC_APP_MASTER_KEY']
PORT = int(os.environ['LC_APP_PORT'])


leancloud.init(APP_ID, master_key=MASTER_KEY)

application = engine

if 'LC_LOG_PATH' in os.environ:
    import sys, time

    class WritableObject:
        def write(self, string):
            #s = time.strftime('%Y-%m-%d %H:%M:%S: \n')       
            #open('testLog.txt', 'a').write("%s%s" % (s, string))
            f = os.environ['LC_LOG_PATH']
            f += '/agm_self.log'
            open(f, 'a').write(string)
            
    sys.stdout = WritableObject()

if __name__ == '__main__':
    # 只在本地开发环境执行的代码
    # app.debug = True
    # app.run(port=PORT)
    server = simple_server.make_server('localhost', PORT, application)  
    server.serve_forever()
